<?php 
require_once 'ckLogin.php';
require_once 'init.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();
if(!empty($_GET['act'])){
	$act = $_GET['act'];
	if($act == 'del'){
		$db->sql = "DELETE FROM bottomwords WHERE id = ".$_GET['id'];
		$db->doExecute();
		$rs = 2;
	}
	if($act == "edit"){
		if(strtoupper($_SERVER['REQUEST_METHOD'])=='POST'){
			//updater_edit pdate_edit bwid_edit content
			$id = $_POST['bwid_edit'];
			$updater = $_POST['updater_edit'];
			$content = $_POST['content'];
			$pdate = $_POST['pdate_edit'];
			if (!empty($id)) {
				$db->sql = "UPDATE bottomwords SET `updater` = '{$updater}',`content` = '{$content}',`pdate` = '{$pdate}' WHERE `id` = {$id}";
				$rs = 1;
			}else{
				$db->sql = "INSERT bottomwords(updater,content,pdate) VALUES('{$updater}','{$content}','{$pdate}')";
				$rs = 3;
			}
			$db->doExecute();
			
		}
	}
	if ($act=="huifu") {
		//提交处理
		if(strtoupper($_SERVER['REQUEST_METHOD'])=='POST'){
			$pid=$_POST['pid'];
			$description=htmlspecialchars($_POST['adminReply']);
			//$updater_2 = $_POST['updater_2'];
			//echo $db->sql="UPDATE ".TABLENAME." SET message = '{$description}' , messageid = {$pic_id} , `date` = '"+date("Y-m-d")+"' , openid = 'admin', figureurl = 'http://ctc.qzonestyle.gtimg.cn/qzonestyle/act/qzone_app_img/app100626394_100626394_100.png',nickname='admin',isread=0  WHERE id = ".$pic_id;
			$db->sql="INSERT INTO `message`(`message`,`date`,`openid`,`pid`,`noread`) VALUES('{$description}','".date("Y-m-d")."','{$_POST['openid']}','{$pid}',1)";
			$db->doExecute();
		}
	}
}


// --------------------------------------------------
//
$where = " pid=0";

$page = $_GET['page']?$_GET['page']:1;
$db->sql = "SELECT COUNT(id) FROM message WHERE {$where}";
$total = $db->doResultOne();
$size = 20;
$pageArray = page($total, $size, $page);

$db->sql = "SELECT * FROM message WHERE {$where} ORDER BY id DESC LIMIT {$pageArray['start']},{$size}";
$bottomwords = $db->doResultList2();

foreach ($bottomwords as $key => $item){
	$db->sql = "SELECT * FROM message WHERE pid={$item['id']}";
	$bottomwords[$key]['children'] = $db->doResultList2();
}

//
$url= "?startTime=".$_GET['startTime']."&endTime=".$_GET['endTime']."&nick=".$_GET['nick']."&pagesize=".$_GET['pagesize']."&is_check=".$_GET['is_check'];

//分页
function page($total,$size,$page){
	//最大页数
	if($total%$size==0){
		$maxPage = $total/$size;
	}else{
		$maxPage = (int)($total/$size)+1;
	}
	//开始条数
	$start = ($page-1)*$size;
	//分页
	if($maxPage>=5){
		if($page<=2){
			$pages=array(1,2,3,4,5);
		}elseif($page>=($maxPage-1)){
			$pages=array($maxPage-4,$maxPage-3,$maxPage-2,$maxPage-1,$maxPage);
		}else{
			$pages=array($page-2,$page-1,$page,$page+1,$page+2);
		}
	}else{
		for($i=1;$i<=$maxPage;$i++){
			$pages[]=$i;
		}
	}
	if($page<=1){$previous=1;}else{$previous=$page-1;}
	if($page>=$maxPage) {$next = $maxPage;}else{ $next = $page+1;}
	return array('maxPage'=>$maxPage,'start'=>$start,'pages'=>$pages,'previous'=>$previous,'next'=>$next);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo APPLICATION;?></title>
<!--                       CSS                       -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css" media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/main.css" type="text/css" media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css" media="screen" />
<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.7.2.min.js"></script>
<!-- jQuery Configuration -->
<script type="text/javascript"
	src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>
<!-- jQuery colorbox -->
<link rel="stylesheet" href="resources/css/colorbox.css" />
<script src="resources/scripts/colorbox/jquery.colorbox.js"></script>
<script type="text/javascript">
$(function($) {
	$(".group-pic").colorbox({rel:'group-pic',width:"30%", height:"50%"});
});
</script>
<script type="text/javascript" src="resources/scripts/My97DatePicker/WdatePicker.js"></script>
<script charset="utf-8" src="resources/scripts/kindeditor/kindeditor-min.js"></script>
<script charset="utf-8" src="resources/scripts/kindeditor/lang/zh_CN.js"></script>
<script type="text/javascript">
$(function($) {
	$("form").submit(function(){
		/* if( !$("input[name='userName']").val() || !$("input[name='newPwd']").val() || !$("input[name='againPwd']").val() || !$("input[name='nickName']").val() ){
			alert("都为必填项，不能为空！");
			return false;
		} */
	});
	//删除
	$(".deleteUser").click(function(){
		if(!confirm("是否将此留言删除?")){
			return false;
		}
	});
	//修改
	$(".editcoupon").click(function(){
		$("#messages1").find("input[type='submit']").val("修改");
		var username = $(this).parents("tr").children("td").eq(1).children("span").text();
		var pdate = $(this).parents("tr").children("td").eq(3).text();
		var content = $(this).parents("tr").children("td").eq(4).html();
		var bwid = $(this).parents("tr").children("td").eq(0).text();
		$("#updater_edit").val(username);
		$("#pdate_edit").val(pdate);
		$("#bwid_edit").val(bwid);
		editor.insertHtml(content);
	});
	//回复
	$(".huiufbutton").click(function(){
		var pid = $(this).data("pid");
		//alert(pid);
		$("#pid").val(pid);
		$("#openid").val($(this).data("openid"));
		$("#message_click").click();
	});
	$(".status").click(function(){
		//var status = $(this).children('span').text();
		var id = $(this).parents("tr").children("td").eq(0).text();
		var this1 = $(this);
		$.ajax({
			type: "GET",
			url: "userStatusChange.php",
			data: "id="+id+"&",
			dataType: "html",
			success: function(date){
				if(date==1){
					this1.html('<span style="color:black;">启用</span>');
				}else{
					this1.html('<span style="color:gray;">禁用</span>');
				}
			}
		});
	});
	$(".success").ajaxSuccess(function(evt, request, settings){
		$(this).show();
	});
	$(".error").ajaxError(function(event,request, settings){
		$(this).show();
	});
	$(".add").click(function(){
		//alert(11);
		
		$("#updater_edit").val('');
		$("#pdate_edit").val('');
		$("#bwid_edit").val('');
		editor.html('');
		$("#messages1").find("input[type='submit']").val("新增");
	});
});
var editor;
KindEditor.ready(function(K) {
	editor = K.create('textarea[name="content"]', {
		themeType : 'simple',
		resizeType : 1,
		allowPreviewEmoticons : false,
		allowImageUpload : 0,
		items : [
			'source', '|', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
			'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
			'insertunorderedlist', '|', 'emoticons', 'link']
	});
});
</script>
</head>

<body>
	<div id="body-wrapper">
		<!-- Wrapper for the radial gradient background -->
		<a href="#messages" rel="modal" id="message_click"></a>
		<div id="messages" style="display: none;">
			<!-- Messages are shown when a link with these attributes are clicked: href="#messages" rel="modal"  -->

			<p>
				<strong>回复</strong>
			</p>
			<form action="<?php echo $url."&page=".$page."&act=huifu";?>" method="post" style="text-align: right;">
				<fieldset>
					<textarea class="textarea" id="adminReply" name="adminReply" cols="79" rows="5"></textarea>
					<input id="pid" name="pid" type="hidden" value="" />
					<input id="openid" name="openid" type="hidden" value="" />
					<input class="button" type="submit" value="提交" />
					<a href="javascript:void(0);" onclick="$(this).parents('#facebox').hide();">关闭</a>
				</fieldset>
			</form>

		</div>
		<!-- End #messages -->
		<div id="main-content">

			<div class="content-box">
				<!-- Start Content Box -->

				<div class="content-box-content">

				<!-- Start Notifications -->
				<?php if (isset($rs)):if ($rs==1 || $rs==2 || $rs==3)?>
				<div class="notification success png_bg" style="">
					<a href="#" class="close"><img
						src="resources/images/icons/cross_grey_small.png"
						title="Close this notification" alt="close" /></a>
					<div>
						<?php if ($rs==1) echo '修改';elseif ($rs==3) echo '新增';else echo '删除';?>成功！！！
					</div>
				</div>
				<?php endif;?>
				<div class="notification error png_bg" style="display: none;">
					<a href="#" class="close"><img
						src="resources/images/icons/cross_grey_small.png"
						title="Close this notification" alt="close" /></a>
					<div>
					修改失败
					</div>
				</div>
				
				<!-- End Notifications -->
				
					<div class="tab-content default-tab" id="tab1">
						<!-- This is the target div. id must match the href of this div's tab -->
						<table>
							<thead>
								<tr>
									<td colspan="4">
										<div class="bulk-actions align-left">
											<!-- <button class="button add" >新增</button> -->
										</div>
										<?php if($pageArray['maxPage']>0):?>
										<div class="pagination">
											<a href="?1=1&page=1" title="First Page">&laquo; First</a>
											<a href="<?php echo "?1=1&page=".$pageArray['previous'];?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pageArray['pages'] as $item):?> 
											<a href="<?php echo "?1=1&page=".$item;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="<?php echo "?1=1&page=".$pageArray['next'];?>" title="Next Page">Next &raquo;</a>
											<a href="<?php echo "?1=1&page=".$pageArray['maxPage'];?>" title="Last Page">Last &raquo;</a>
											total : <?php echo $pageArray['maxPage'];?>
										</div> 
										<?php endif;?>
										<!-- End .pagination -->
										<hr class="clear" />
									</td>
								</tr>
							</thead>
							<thead>
								<tr>
									<th>编号</th>
									<th>日期</th>
									<th style="width: 50%">内容</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody>
							<?php if(count($bottomwords)>0):?>
							<?php foreach ($bottomwords as $key=>$item):?>
								<tr>
									<td style="font-weight: bold;font-size: medium;"><?php echo $item['id'];?></td>
									<td><?php echo $item['date'];?></td>
									<td><?php echo $item['message'];?></td>
									<td>
									<?php if($item['openid']):?>
										<a class="huiufbutton" href="javascript:void(0);" title="回复" data-pid="<?php echo $item['id'];?>" data-openid="<?php echo $item['openid'];?>">回复</a>
									<?php endif;?>
									<!-- 
										<a class="editcoupon" href="javascript:void(0);">修改</a>
										<a class="deleteUser" href="?id=<?php echo $item['id'];?>&act=del">删除</a> -->
									</td>
								</tr>
								<?php foreach ($item['children'] as $key2=>$item2):?>
								<tr>
									<td></td>
									<td><?php echo $item2['date'];?></td>
									<td><?php echo $item2['message'];?></td>
									<td>
									
									<!-- 
										<a class="editcoupon" href="javascript:void(0);">修改</a>
										<a class="deleteUser" href="?id=<?php echo $item['id'];?>&act=del">删除</a> -->
									</td>
								</tr>
								<?php endforeach;?>
							<?php endforeach;?>
							<?php else:?>
								<tr style="font-size: 50px;text-align: center; font-weight: bold;"><td colspan="9" style="text-align: center;">没有留言！！</td></tr>
							<?php endif;?>
							</tbody>
							<tfoot>
								<tr>
									<td colspan="4">
										<div class="bulk-actions align-left">
											<!-- <button class="button add" >新增</button> -->
										</div>
										<?php if($pageArray['maxPage']>0):?>
										<div class="pagination">
											<a href="?1=1&page=1" title="First Page">&laquo; First</a>
											<a href="<?php echo "?1=1&page=".$pageArray['previous'];?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pageArray['pages'] as $item):?> 
											<a href="<?php echo "?1=1&page=".$item;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="<?php echo "?1=1&page=".$pageArray['next'];?>" title="Next Page">Next &raquo;</a>
											<a href="<?php echo "?1=1&page=".$pageArray['maxPage'];?>" title="Last Page">Last &raquo;</a>
											total : <?php echo $pageArray['maxPage'];?>
										</div> 
										<?php endif;?>
										<!-- End .pagination -->
										<div class="clear"></div>
									</td>
								</tr>
							</tfoot>
						</table>

					</div>
					<!-- End #tab2 -->

				</div>
				<!-- End .content-box-content -->

			</div>
			<!-- End .content-box -->

			<div class="clear"></div>

			<div id="footer">
				<small> <!-- Remove this notice or replace it with whatever you want -->
					&#169; Copyright 2009 Your Company | Powered by <a
					href="http://themeforest.net/item/simpla-admin-flexible-user-friendly-admin-skin/46073">Simpla
						Admin</a> | <a href="#">Top</a>
				</small>
			</div>
			<!-- End #footer -->

		</div>
		<!-- End #main-content -->

	</div>
</body>


<!-- Download From www.exet.tk-->
</html>
